import Vue from "vue";
import Vuex from "vuex"; // 引入Vuex
import { getMusicDetail, musicLyric } from "@/api/api.js";
Vue.use(Vuex); // 应用Vuex插件
const actions = {
  //获取音乐信息
  getMusic(context, value) {
    getMusicDetail(value).then(
      (response) => {
        //console.log(response.data);
        context.commit("GETMUSIC", response.data.songs[0]);
        context.commit("ADDONE",response.data.songs[0])
        console.log(response.data.songs[0]);
      },
      (error) => {
        console.log(error.message);
      }
    );
  },
  //获取歌词信息
  musicLyric(context, value) {
    musicLyric(value).then(
      (response) => {
        context.commit("MUSICLYRIC", response.data.lrc.lyric);
      },
      (error) => {
        console.log(error.message);
      }
    );
  },
}; // 准备actions——用于响应组件中的动作
const mutations = {
  // 歌词信息
  MUSICLYRIC(state, value) {
    state.musicLyric = value;
  },
  CHANGEMUSIC(state, value) {
    state.audiosrc = value;
  },
  STARTMUSIC(state, value) {
    state.isaudio = value;
  },
  MUSICID(state, value) {
    state.musicId = value;
  },
  GETMUSIC(state, value) {
    state.musicDetail = value;
  },
  AUDIO(state, value) {
    state.audio = value;
  },
  //推荐歌单id
  RECOMMENDID(state, value) {
    state.recommendId = value;
  },
  // 点击返回
  ISRE(state, value) {
    state.isre = value;
  },
  //推荐歌单信息
  RECOMMENDSONGLIST(state, value) {
    state.recommendSonglist = value;
  },
  //播放歌曲在推荐歌单中的索引
  INDEX(state, value) {
    state.index = value;
  },
  //将歌曲添加到列表
  ADDALL(state, value) {
    state.playList.unshift(...value);
  },
  ADDONE(state, value) {
    var isadd=true
    state.playList.forEach(item=>{
      if(item.id==value.id){
        isadd=false;
      }
    })
    if(isadd){
      state.playList.unshift(value);
    }
  },
}; // 准备mutations——用于操作数据（state）
const state = {
  // 列表内歌曲
  playList: [],
  // 音乐地址
  audiosrc: "https://music.163.com/song/media/outer/url?id=1922374770",
  //推荐歌单id
  recommendId: "",
  //推荐歌单信息
  recommendSonglist: "",
  //播放歌曲在推荐歌单中的索引
  index: 0,
  //歌词信息
  musicLyric: "",

  isre: true,
  isaudio: false,
  musicId: 1922374770,
  musicDetail: {
    name: "Upupu",
    ar: [{ name: "Upupu" }],
    dt: "皮皮爱习题",
    al: {
      picUrl:
        "https://p2.music.126.net/Zx3qF7tN5vWGUQo_0H9R7Q==/109951167180588193.jpg",
    },
  },
}; 
export default new Vuex.Store({
  actions,
  mutations,
  state,
});
